/**
 * 常用工具函数
 */
import wepy from 'wepy'

/**
*  手机验证
*/
function validateTel (tel) {
  let reg = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/i
  let bool = reg.test(tel)
  if (!bool) {
    wepy.showToast({
      title: '请输入正确的手机号',
      icon: 'none'
    })
  }
  return bool
}

/**
 *  字符串非空验证
 */
function validateEmp (str, msg) {
  let length = str.replace(/(^\s*)|(\s*$)/g, '').length
  if (!length) {
    wepy.showToast({
      title: `${msg}不能为空`,
      icon: 'none'
    })
    return false
  }
  return true
}

/**
 *  时间戳转换时间
 */
function timestampToTime (timestamp) {
  let date = new Date(timestamp * 1000)
  let Y = date.getFullYear() + '-'
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  let D = date.getDate() + ' '
  // h = date.getHours() + ':'
  // m = date.getMinutes() + ':'
  // s = date.getSeconds()
  return Y + M + D
}

export {
  validateTel,
  validateEmp,
  timestampToTime
}
